package com.jinmei.mockapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class MockAppRequest {
	
	 private HttpClient httpClient;
	 private HttpPost httpRequest;
     private HttpResponse response;
     
     private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
	
	public void sendRequest(String url) throws Exception {
        httpClient = new DefaultHttpClient();
        httpRequest = new HttpPost(url);
        httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));
        response = httpClient.execute(httpRequest);
    }
	
	public String getResponse() throws Exception {
		StringBuilder sb = new StringBuilder();
		
		if (response.getStatusLine().getStatusCode() == 200) {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));
            for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
                    .readLine()) {
                sb.append(s);
            }
        }
        return sb.toString();
    }
	
	public void addNameValuePair(String key, String value) {
        nameValuePair.add(new BasicNameValuePair(key, value));
    }
	
}
